/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.classdiagram.editpart;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.palette.PaletteDrawer;
import org.eclipse.uml2.uml.Association;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.ElementImport;
import org.eclipse.uml2.uml.Extension;
import org.eclipse.uml2.uml.Generalization;
import org.eclipse.uml2.uml.Stereotype;

import com.cea.papyrus.core.PapyrusMessages;
import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.directedit.IDirectEdition;
import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.editor.mouseoveraction.ActionHandleManager;
import com.cea.papyrus.core.figures.ClassifierFigure;
import com.cea.papyrus.core.figures.IconFigure;
import com.cea.papyrus.core.figures.NodeFigure;
import com.cea.papyrus.core.ui.dialogs.PapyrusMessageDialog;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

/**
 * Controller class for {@link Stereotype}.
 */
public class StereotypeEditPart extends ClassEditPart {

	/**
	 * Creates a new StereotypeEditPart.
	 * @param modelManager manager in charge of models, graphic and domain
	 * @param graphNode parent of the graph node controlled by this edit part
	 */
	public StereotypeEditPart(GraphNode graphNode,ModelManager modelManager) {
		super(graphNode,modelManager);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void installActionHandle() {
		// Install mouse hover
		ActionHandleManager actionMngr = new ActionHandleManager(this);	
		// Create a toolbar of actions.
		PaletteDrawer entries = new PaletteDrawer("creation tool");
		try{
			entries.add(PapyrusPlugin.creationToolFactory.getConnectionCreationToolEntry(
					PapyrusMessages.AddExtensionCommand_Label, 
					PapyrusMessages.AddExtensionCommand_Description, 
					Extension.class, 
					null));
			entries.add(PapyrusPlugin.creationToolFactory.getConnectionCreationToolEntry(
					PapyrusMessages.AddGeneralizationCommand_Label,
					PapyrusMessages.AddGeneralizationCommand_Description,
					Generalization.class,
					null));
			entries.add(PapyrusPlugin.creationToolFactory.getConnectionCreationToolEntry(
					PapyrusMessages.AddAssociationCommand_Label,
					PapyrusMessages.AddAssociationCommand_Description,
					Association.class,
					null));
		} catch (Exception e) {
			PapyrusMessageDialog.display(e.getMessage(), e);
		}
		actionMngr.addTool(entries, PositionConstants.EAST, false, 5);
	}			

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected IFigure createFigure() {
		// Force initialization of the manager when figure has changed
		manager = null;	
		directEditionMode = IDirectEdition.UNDEFINED_DIRECT_EDITOR;
		//  Try to get figure from ContainerWithSemanticEditPart
		IFigure fig = super.createFigure();
		if (fig != null) {
			return fig;
		}
		// else
		return new ClassifierFigure();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public org.eclipse.uml2.uml.Stereotype getUmlElement() {
		Element e=((Uml1SemanticModelBridge)getGraphNode().getSemanticModel()).getElement();
		if(e instanceof ElementImport){
			return (org.eclipse.uml2.uml.Stereotype)((ElementImport)e).getImportedElement();
		}
		return (org.eclipse.uml2.uml.Stereotype)e;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void refreshVisuals() {
		Dimension size = getGraphNode().getSize();
		Point p = getGraphNode().getPosition();
		Rectangle r = new Rectangle(p ,size);

		IFigure fig = getFigure();

		// if fig is a stereotype figure
		if (!(fig instanceof ClassifierFigure)) {
			IconFigure icf= (IconFigure)getFigure();
			icf.setName(getUmlElement().getName());

		} else {
			// Retrieve current class and figure
			org.eclipse.uml2.uml.Stereotype currentStereotype = getUmlElement();
			com.cea.papyrus.umlutils.Element currentElement = new com.cea.papyrus.umlutils.Element(currentStereotype);

			ClassifierFigure f = (ClassifierFigure) fig; //get Class figure

			// if fig is a ClassFigure
			((NodeFigure) fig).refreshColors(getGraphNode());
			((NodeFigure) fig).refreshPresentation(getPresentation(), currentStereotype, getGraphNode());
			boolean hasIcon  = currentElement.hasIcon();
			boolean hasShape = currentElement.hasShape();
			//f.refreshStereotypes(currentElement.getStereotypes(), getPresentation(), hasIcon, hasShape);
			f.refreshStereotypes(f.stereotypesLabel(this), getPresentation(), hasIcon, hasShape);

			f.setName(currentStereotype.getName()); // set the name of the class

			//qualifiedName
			GraphNode gp= (GraphNode)getGraphNode().getContainer();
			f.refreshQualifiedName(gp,getGraphNode(), currentStereotype);

			//setAbstract
			f.setAbstract(currentStereotype.isAbstract());//Is-it abstract?
		}
		((GraphicalEditPart) getParent()).setLayoutConstraint(this, fig, r);
	}

//	/**
//	 * Returns the Figure of this as a PackageFigure.
//	 * @return PackageFigure of this.
//	 */
//	protected ClassifierFigure getPackageBoardFigure() {
//		return (ClassifierFigure)getFigure();
//	}
}
